﻿//----------------------------------------------------------------------------------------------------
// GameType_Normal, go get the goal node, thats it.
//----------------------------------------------------------------------------------------------------

package HG.GameTypes
{
	import HG.Debug;
	import HG.GameService;
	
	import HG.Managers.GameRuleManager;
	import HG.Managers.LevelManager;
	import HG.Managers.NodeManager;
	import HG.Managers.TurnManager;
	
	import HG.Events.TurnEvent;
	import HG.Events.NodeEvent;
	import HG.Managers.PlayerManager;
	
	public class GameType_Rush extends GameType_Normal
	{
		var m_turnManager:TurnManager;
		var m_playerManager:PlayerManager;
		
		var m_turnsToDisconnect:Number;
		
		//----------------------------------------------------------------------------------------------------
		public function GameType_Rush()
		{
			super();
			
			m_name = "Rush";
			m_description = "Reach the Goal Node before it goes offline. Capture Generators to keep it online for an aditional turn.";
			
			m_gameRuleManager = GameService.GetInstance().GetGameRuleManager();
			m_levelManager = GameService.GetInstance().GetLevelManager();
			m_turnManager = GameService.GetInstance().GetTurnManager();
			m_playerManager = GameService.GetInstance().GetPlayerManager();
			
			m_levelManager.addEventListener(NodeEvent.CAPTURE, OnRushNodeCaptured);
			m_turnManager.addEventListener(TurnEvent.END, OnTurnEnd);
			
			m_turnsToDisconnect = 3;
		}
		
		//----------------------------------------------------------------------------------------------------
		public function OnRushNodeCaptured(evt:NodeEvent)
		{
			if (!m_active)
				return;
				
			if (m_gameRuleManager.GetEditorMode())
				return;
			
			switch (evt.m_node.GetType())
			{
				// Goal node for winning --------------------------------
				case NodeManager.NODE_GOAL:
				{
					m_gameRuleManager.DeclarePlayerFate(GameRuleManager.FATE_VICTORY, evt.m_capBy);
				}break;
				
				// Generators for delaying the inevitable
				case NodeManager.NODE_GENERATOR:
				{
					m_turnsToDisconnect += 3;
					m_gameRuleManager.RaiseGameUpdateEvent();
				}break;
			}
		}
		
		//----------------------------------------------------------------------------------------------------
		public function OnTurnEnd(evt:TurnEvent)
		{
			if (!m_active)
				return;
				
			var currentPlayerID:Number = m_playerManager.GetCurrentPlayerID();
			
			// only count your own turns.
			if (evt.m_inputPlayerID != currentPlayerID)
				return;
				
			if (m_turnsToDisconnect <= 0)
			{
				m_gameRuleManager.DeclarePlayerFate(GameRuleManager.FATE_DEFEAT, currentPlayerID);
			}
			
			m_turnsToDisconnect--;
			m_gameRuleManager.RaiseGameUpdateEvent();
		}
		
		//----------------------------------------------------------------------------------------------------
		public function GetTurnsToDisconnect():Number
		{
			return m_turnsToDisconnect;
		}
		
		//----------------------------------------------------------------------------------------------------
	}
}